package com.diao.myhub.util;

import lombok.experimental.UtilityClass;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Optional;

@UtilityClass
public class RequestUtils {
    public String getDomain() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return Optional.of(requestAttributes.getRequest())
                .map(req -> req.getHeader("Referer"))
                .map(ref -> ref.split("\\?")[0])
                .map(url -> url.endsWith("/") ? url.substring(0, url.length() - 1) : url)
//                .map(request -> request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort())
                .orElse("");
    }
}
